<?php
/*
Plugin Name: WP-prettyPhoto
Plugin URI: http://blog.fusi0n.org/coding/wp-prettyphoto-lightbox-clone-for-wordpress-using-jquery
Description: WP-prettyPhoto allows you to use <a href="http://www.no-margin-for-errors.com/projects/prettyPhoto/" alt="prettyPhoto">prettyPhoto</a> by <a href="http://www.no-margin-for-errors.com/">Stéphane Caron</a>, a jQuery-powered Lightbox clone with WordPress.
Version: 1.1
Author: Pier-Luc Petitclerc
Author URI: http://blog.fusi0n.org
*/



/**
 * Adds a 'Settings' link to the plugins row
 *
 * @param array $links List of plugins to be displayed in the page
 * @param string $file Filename of the plugin currently being parsed/displayed
 * @return array Same thing as $links, but with our Settings link with it.
 * @example add_action('admin_head', 'wppp_add_options_page');
 * @author Joost de Valk
 * @see http://yoast.com/wordpress/enhanced-wordpress-contact-form/
 * @since 1.0
 */
function wppp_admin_hook($links, $file) {
  static $this_plugin;
  if (!$this_plugin) { $this_plugin = plugin_basename(__FILE__); }
  if ($file == $this_plugin) {
    $settings_link = '<a href="options-general.php?page='.plugin_basename(dirname(__FILE__)).'/options-wppp.php">' . __('Settings') . '</a>';
    array_unshift( $links, $settings_link ); // before other links
  }
  return $links;
}

/**
 * Adds the plugin's option page in the 'Settings' menu of the Admin
 * @param null
 * @return void
 * @global string $wppp_path
 * @author Rupert Morris
 * @since 1.0
 */
function wppp_add_options_page() {
  global $wppp_path;
  add_options_page('WP-prettyPhoto Options', 'WP-prettyPhoto', 'manage_options', $wppp_path.'options-wppp.php');
  add_filter( 'plugin_action_links', 'wppp_admin_hook', 10, 2 );
}


/**
 * Replaces occurences of $wppp_rel with the prettyPhoto rel hook
 * @param string $content Post/page contents
 * @return string Prettified post/page contents
 * @global object $post
 * @author Rupert Morris
 * @author Tanin "Regular Expression God"
 * @see http://0xtc.com/2008/05/27/auto-lightbox-function.xhtml
 * @since 1.0
*/
function wppp_content_hook($content) {
  global $post;
  $wppp_rel         = get_option('wppp_rel');
  $pattern['0']     = "/<a(.*?)href=('|\")([A-Za-z0-9\/_\.\~\:-]*?)(\.bmp|\.gif|\.jpg|\.jpeg|\.png)('|\")([^\>]*?)>/i";
  $pattern['1']     = "/<a(.*?)href=('|\")([A-Za-z0-9\/_\.\~\:-]*?)(\.bmp|\.gif|\.jpg|\.jpeg|\.png)('|\")(.*?)(rel=('|\")$wppp_rel(.*?)('|\"))([ \t\r\n\v\f]*?)((rel=('|\")$wppp_rel(.*?)('|\"))?)([ \t\r\n\v\f]?)([^\>]*?)>/i";
  $replacement['0'] = '<a$1href=$2$3$4$5$6 rel="'.$wppp_rel.'[g'.$post->ID.']">';
  $replacement['1'] = '<a$1href=$2$3$4$5$6$7>';
  $content          = preg_replace($pattern, $replacement, $content);
  return $content;
}

/**
 * Adds prettyPhoto in the page that is being viewed
 * @param null
 * @return void
 * @global string $wppp_url
 * @author Pier-Luc Petitclerc <plpetitclerc@gmail.com>
 * @example add_action('wp_head', 'wppp_styles');
 * @see http://www.no-margin-for-errors.com/projects/prettyPhoto/#howtouse
 * @since 1.0
 */
function wppp_styles() {
  global $wppp_url;
  $wppp_rel           = get_option('wppp_rel');
  $wppp_speed         = get_option('wppp_speed');
  $wppp_padding       = get_option('wppp_padding');
  $wppp_opacity       = get_option('wppp_opacity');
  $wppp_title         = get_option('wppp_title') == 'true'? 'true' : 'false';
  $wppp_resize        = get_option('wppp_resize') == 'true'? 'true' : 'false';
  $wppp_counterlabel  = get_option('wppp_counterlabel');
  $wppp_theme         = get_option('wppp_theme');

  $ie6 = <<<EOHEAD
    <!--[if IE 6]>
	<script>
		DD_belatedPNG.fix('div.pictureHolder .top .left,div.pictureHolder .top .right,div.pictureHolder .top .left,div.pictureHolder .bottom .left,div.pictureHolder .bottom .right,div.pictureHolder .content a.close,div.pictureHolder .content .details .nav a.arrow_next,div.pictureHolder .content .details .nav a.arrow_previous,div.pictureHolder .content,div.pictureHolder .bottom .middle,div.pictureHolder .top .middle'); 
	</script>
    <![endif]-->
EOHEAD;

  $output = <<<EOHTML
    <script type="text/javascript" charset="utf-8">
      $(document).ready(function(){
        $("a[rel^='{$wppp_rel}']").prettyPhoto({
          animationSpeed: '{$wppp_speed}', /* fast/slow/normal */
          padding: {$wppp_padding}, /* padding for each side of the picture */
          opacity: {$wppp_opacity}, /* Value betwee 0 and 1 */
          showTitle: {$wppp_title}, /* true/false */
          allowresize: {$wppp_resize}, /* true/false */
          counter_separator_label: '{$wppp_counterlabel}', /* The separator for the gallery counter 1 "of" 2 */
          theme: '{$wppp_theme}' /* light_rounded / dark_rounded / dark_square */
        });
      });
    </script>
EOHTML;
  echo ((strpos($wppp_theme, 'dark_') !== false) && (is_ie6()))? $ie6.$output : $output;;
}

/**
 * Fetches a list of installed prettyPhoto themes
 * @param null
 * @return array Numeric array containing directory names of installed themes
 * @global string $wppp_path
 * @author Pier-Luc Petitclerc <plpetitclerc@gmail.com>
 * @since 1.1
 */
function wppp_get_themes() {
  global $wppp_path;
  $themes = array();
  $dir = scandir($wppp_path.'images/prettyPhoto');
  foreach ($dir as $key=>$dirname) {
    if ((strlen($dirname) > 2) && is_dir($wppp_path.'images/prettyPhoto/'.$dirname)) { $themes[] = $dirname; }
  }
  return $themes;
}

/**
 * Detects if user is using Microsoft Internet Explorer 6
 * @param null
 * @return bool True if user is using MSIE6, False if he is not
 * @author Pier-Luc Petitclerc <plpetitclerc@gmail.com>
 * @since 1.1
 */
function is_ie6() {
  return strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6') !== false? true : false;
}

/*
// Is is_mobile really worth caching?
function is_mobile_cached() {
  if (defined('ENABLE_CACHE')) {
    $cachekey = is_user_logged_in()? md5('isMobile-'.get_profile('ID')) : md5($_SERVER['REMOTE_ADDR'].$_SERVER['HTTP_USER_AGENT']);
    if ($ret=wp_cache_get($cachekey) !== false) { return $ret; }
    else { wp_cache_set($cachekey, is_mobile()); }
  }
}
*/

/**
 * Detects if user is using a known mobile browser
 * @param null
 * @return bool True if user is using a mobile browser, false if he is not
 * @author Pier-Luc Petitclerc <plpetitclerc@gmail.com>
 * @see http://detectmobilebrowsers.mobi
 * @since 1.1
 */
function is_mobile() {
  $browsers = array('ipod', 'iphone', 'android', 'opera mini', 'blackberry');
  $regex = '('.implode($browsers, ')|(').')';
  return ((eregi($regex, $_SERVER['HTTP_USER_AGENT'])) || 
      (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|pda|psp|treo)/i',$_SERVER['HTTP_USER_AGENT'])) || 
      ((strpos($_SERVER['HTTP_ACCEPT'],'text/vnd.wap.wml') !== false) || (strpos($_SERVER['HTTP_ACCEPT'],'application/vnd.wap.xhtml+xml') !== false)) || 
      (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE']))) ? true : false;
}






// Default values
add_option('wppp_automate', 1);
add_option('wppp_rel', 'prettyPhoto');
add_option('wppp_speed', 'normal');
add_option('wppp_padding', 40);
add_option('wppp_opacity', 0.35);
add_option('wppp_title', 'true');
add_option('wppp_resize', 'true');
add_option('wppp_counterlabel', '/');
add_option('wppp_theme', 'dark_rounded');

if (!defined('WP_CONTENT_URL')) { define('WP_CONTENT_URL', get_option('wpurl').'/wp-content'); }
if (!defined('WP_CONTENT_DIR')) { define('WP_CONTENT_DIR', ABSPATH.'wp-content'); }
define('PRETTYPHOTO_VER', '2.2.3');

$wppp_path    = WP_CONTENT_DIR.'/plugins/'.plugin_basename(dirname(__FILE__).'/');
$wppp_options = WP_CONTENT_URL.'/plugins'.plugin_basename(dirname(__FILE__)).'/options-wpp.php';
$wppp_url     = WP_CONTENT_URL.'/plugins/'.plugin_basename(dirname(__FILE__));

if (get_option('wppp_automate') == 1){
  add_filter('the_content', 'wppp_content_hook', 99);
  add_filter('the_excerpt', 'wppp_content_hook', 99);
}

if (!is_admin() && !is_mobile()) {
  // jQuery - removing to make sure we're using 1.3.1
  wp_deregister_script('jquery');
  wp_register_script('jquery', ("$wppp_url/js/jquery-1.3.1.min.js"), false, '1.3.1');
  wp_enqueue_script('jquery');

  // prettyPhoto JavaScript
  wp_register_script('prettyphoto', ("$wppp_url/js/jquery.prettyPhoto.js"), array('jquery'), PRETTYPHOTO_VER);
  wp_enqueue_script('prettyphoto');
  // prettyPhoto CSS
  wp_enqueue_style('prettyphoto', ("$wppp_url/css/prettyPhoto.css"), false, PRETTYPHOTO_VER);

  // Enable PNG transparency hook if we're running with IE6
  if (is_ie6() !== false) {
    wp_register_script('belatedpng', ("$wppp_url/js/DD_belatedPNG_0.0.7a-min.js"), array('prettyphoto'), '0.0.7a');
    wp_enqueue_script('belatedpng');
  }
}

add_action('wp_head', 'wppp_styles');
add_action('admin_head', 'wppp_add_options_page');
?>
